<script>
  $(document).ready(function(){
    <?php foreach ($roles as $role):?>
    $('#<?php echo $role['id'];?>').click(function(){

        if($('#<?php echo $role['id'];?>').is(":checked")){

            $('#c<?php echo $role['id'];?>').prop('checked', true).prop('disabled', false);
            $('#r<?php echo $role['id'];?>').prop('checked', true).prop('disabled', false);
            $('#u<?php echo $role['id'];?>').prop('checked', true).prop('disabled', false);
            $('#d<?php echo $role['id'];?>').prop('checked', true).prop('disabled', false);

            $('#cre<?php echo $role['id'];?>').val('1');
            $('#rea<?php echo $role['id'];?>').val('1');
            $('#upd<?php echo $role['id'];?>').val('1');
            $('#del<?php echo $role['id'];?>').val('1');            

            $('#cre<?php echo $role['id'];?>').attr('name','c[]');
            $('#rea<?php echo $role['id'];?>').attr('name','r[]');
            $('#upd<?php echo $role['id'];?>').attr('name','u[]');
            $('#del<?php echo $role['id'];?>').attr('name','d[]');

        }else{

            $('#c<?php echo $role['id'];?>').prop('checked', false).prop('disabled', true);
            $('#r<?php echo $role['id'];?>').prop('checked', false).prop('disabled', true);
            $('#u<?php echo $role['id'];?>').prop('checked', false).prop('disabled', true);
            $('#d<?php echo $role['id'];?>').prop('checked', false).prop('disabled', true);

            $('#cre<?php echo $role['id'];?>').val('0');
            $('#rea<?php echo $role['id'];?>').val('0');
            $('#upd<?php echo $role['id'];?>').val('0');
            $('#del<?php echo $role['id'];?>').val('0');

            $('#cre<?php echo $role['id'];?>').removeAttr('name');
            $('#rea<?php echo $role['id'];?>').removeAttr('name');
            $('#upd<?php echo $role['id'];?>').removeAttr('name');
            $('#del<?php echo $role['id'];?>').removeAttr('name');
            
        }
    });

    $('#c<?php echo $role['id'];?>').click(function(){
        if($(this).is(":checked")){
            $('#cre<?php echo $role['id'];?>').val('1');
        }else{
            $('#cre<?php echo $role['id'];?>').val('0');
        }
    });
    $('#r<?php echo $role['id'];?>').click(function(){
        if($(this).is(":checked")){
            $('#rea<?php echo $role['id'];?>').val('1');
        }else{
            $('#rea<?php echo $role['id'];?>').val('0');
        }
    });
    $('#u<?php echo $role['id'];?>').click(function(){
        if($(this).is(":checked")){
            $('#upd<?php echo $role['id'];?>').val('1');
        }else{
            $('#upd<?php echo $role['id'];?>').val('0');
        }
    });
    $('#d<?php echo $role['id'];?>').click(function(){
        if($(this).is(":checked")){
            $('#del<?php echo $role['id'];?>').val('1');
        }else{
            $('#del<?php echo $role['id'];?>').val('0');
        }
    });
    <?php endforeach;?>
  });
</script>

    <div class="container">
      <div class="bg-putih">

        <div class="jdl-page">
          <?php echo isset($judul)?$judul:'<font color="#ccc">Judul Belum Diberikan</font>'; ?>
          <div class="clearfix"></div>
        </div>
        
        <?php echo display_flash(); ?>
        
        <?php echo form_open(current_url(), array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form')); ?>
          <?php echo form_input($group_name_hide);?>
          <div class="form-group">
            <label class="col-xs-3">Nama Grup</label>
            <div class="col-xs-4">
              <?php echo form_input($group_name);?>
            </div>
          </div>
          <div class="form-group">
            <label class="col-xs-3">Deskripsi</label>
            <div class="col-xs-7">
              <?php echo form_input($group_description);?>
            </div>
          </div>
          <div class="form-group">
            <label class="col-xs-3">Role yang diizinkan</label>
            <label class="col-xs-9">Akses yang diberkan</label>
          </div>
          <div class="form-group" style='-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;' unselectable='on' onselectstart='return false;' onmousedown='return false;'>
              
            <?php 
            if ($roles>0) {
              foreach ($roles as $roles):
                $rID=$roles['id'];
                $checked = null;
                $item = null;
                if($currentRoles > 0){
                  foreach($currentRoles as $rls) {
                    if ($rID == $rls['role_id']) {
                      $checked= ' checked="checked"';
                      break;
                    }
                  }
                }
            ?>
              <div class="col-xs-3">
                <div class="checkbox checkbox-inline checkbox-primary">
                  <input type="checkbox" name="roles[]" id="<?php echo $roles['id'];?>" value="<?php echo $roles['id'];?>"<?php echo $checked;?>>
                  <label for="<?php echo $roles['id'];?>"> <?php echo $roles['name'];?></label>
                </div>
              </div>

              <div class="col-xs-9">
               
                <input type="hidden" id="cre<?php echo $roles['id'];?>" <?php if($checked ==' checked="checked"'){echo 'name="c[]"';}?> value="<?php if(isset($rls['rule'])){echo substr($rls['rule'], 0,1);}else{echo '0';}?>">
                <div class="checkbox checkbox-inline checkbox-success" style="margin-right:70px;">
                  <input type="checkbox" id="c<?php echo $roles['id'];?>" <?php if(isset($rls['rule']) && $checked ==' checked="checked"'){if (substr($rls['rule'], 0,1)=='1') { echo 'checked';}}?> <?php if($checked!=' checked="checked"'){echo ' disabled';}?>>
                  <label for="c<?php echo $roles['id'];?>" style="font-family:open_sansregular, arial;"> Create</label>
                </div>

                <input type="hidden" id="rea<?php echo $roles['id'];?>" <?php if($checked ==' checked="checked"'){echo 'name="r[]"';}?>  value="<?php if(isset($rls['rule'])){echo substr($rls['rule'], 1,1);}else{echo '0';}?>">
                <div class="checkbox checkbox-inline checkbox-info" style="margin-right:70px;">
                  <input type="checkbox" id="r<?php echo $roles['id'];?>" <?php if(isset($rls['rule']) && $checked ==' checked="checked"'){if (substr($rls['rule'], 1,1)=='1') { echo 'checked';}}?> <?php if($checked!=' checked="checked"'){echo ' disabled';}?>>
                  <label for="r<?php echo $roles['id'];?>" style="font-family:open_sansregular, arial;"> Read</label>
                </div>


                <input type="hidden" id="upd<?php echo $roles['id'];?>" <?php if($checked ==' checked="checked"'){echo 'name="u[]"';}?> value="<?php if(isset($rls['rule'])){echo substr($rls['rule'], 2,1);}else{echo '0';}?>">
                <div class="checkbox checkbox-inline checkbox-warning" style="margin-right:70px;">
                  <input type="checkbox" id="u<?php echo $roles['id'];?>" <?php if(isset($rls['rule']) && $checked ==' checked="checked"'){if (substr($rls['rule'], 2,1)=='1') { echo 'checked';}}?> <?php if($checked!=' checked="checked"'){echo ' disabled';}?>>
                  <label for="u<?php echo $roles['id'];?>" style="font-family:open_sansregular, arial;"> Update</label>
                </div>

                <input type="hidden" id="del<?php echo $roles['id'];?>" <?php if($checked ==' checked="checked"'){echo 'name="d[]"';}?> value="<?php if(isset($rls['rule'])){echo substr($rls['rule'], 3,1);}else{echo '0';}?>">
                <div class="checkbox checkbox-inline checkbox-danger" style="margin-right:70px;">
                  <input type="checkbox" id="d<?php echo $roles['id'];?>" <?php if(isset($rls['rule']) && $checked ==' checked="checked"'){if (substr($rls['rule'], 3,1)=='1') { echo 'checked';}}?> <?php if($checked!=' checked="checked"'){echo ' disabled';}?>>
                  <label for="d<?php echo $roles['id'];?>" style="font-family:open_sansregular, arial;"> Delete</label>
                </div>

              </div>
            <?php 
              endforeach; 
            }
            ?>
          </div>

          <div class="form-group">
            <div class="col-xs-12">
              <button type="submit" class="btn btn-primary btn-sm">Simpan Perubahan</button>
            </div>
          </div>
        <?php echo form_close(); ?>

        <div class="clearfix"></div>
      </div>
    </div> <!-- /container -->